<?php
session_start();
require('../conn.php');
require('../functions.php');
require('admin.php');
$id = guolv($_GET['id']);
$row = $mysql->query("select * from `article` where `id`='{$id}'");
if(strstr($row[0]['pic'],"http")){
    $img_array[0]=$row[0]['pic'];
}else{
    $img_array = explode(",", $row[0]['pic']);
    $img_array[0]="http://read.html5.qq.com/image?src=fav&imgflag=2&imageUrl=http://".$config['img_url']. $img_array[0];


}
echo $img_array[0];


$_pic = str_replace('[weixin]', $image_protect, $row[0]['pic']);
$picture=$row[0]['pic'];
if (strpos($row[0]['pic'], "[weixin]") !== 0) {
    $_pic = $site . $row[0]['pic'];
}
if ($_POST) {
    $title = guolv($_POST['title']);
    $content = guolv($_POST['content']);
    $extra = guolv($_POST['extra']);
    $type = guolv($_POST['type']);
    $code = guolv($_POST['code']);
    $flag=guolv($_POST['flag']);
    $content = str_replace('http://img01.store.sogou.com/net/a/04/link?appid=100520031&w=710&url=', '', $content);
    $pv_max = guolv($_POST['pv_max']);
    $money = guolv($_POST['money']);
    $aid = guolv($_POST['id']);
    $pic1=$_POST['pic'];
    if(strstr($pic1,"http")){
        $pic_array=$pic1;
    }else{
    $pic2=$_POST['pic2'];
    $pic3=$_POST['pic3'];
    $pic_array=$pic1.",".$pic2.",".$pic3;
    }

//    var_dump($pic);
//    exit;
     $row_up= $mysql->execute("update `article` set `title`='{$title}',`type`={$type},code={$code},flag={$flag},`content`='{$content}',`pic`='{$pic_array}' where `id`='{$aid}'");
   if($row_up) {
       echo "<script>alert('修改成功');location.href='edit_article.php?id={$aid}'</script>";
   }else{
       echo $mysql->error();
       die();
   }
    exit;
} ?>

<?php include('head.php') ?>
<link rel="stylesheet" href="../editor/themes/default/default.css"/>
<link rel="stylesheet" href="../editor/plugins/code/prettify.css"/>
<script src="<?php echo $jquerysite ?>" type="text/javascript"></script>
<script charset="utf-8" src="../editor/kindeditor.js"></script>
<script charset="utf-8" src="../editor/lang/zh_CN.js"></script>
<script charset="utf-8" src="../editor/plugins/code/prettify.js"></script>
<script>
    KindEditor.ready(function (K) {
        var editor1 = K.create('textarea[name="content"]', {
            cssPath: '../editor/plugins/code/prettify.css',
            uploadJson: '../editor/php/upload_json.php',
            fileManagerJson: '../editor/php/file_manager_json.php',
            allowFileManager: true,
            filterMode: false //是否开启过滤模式
        });
        prettyPrint();
    });
</script>
<script>
    KindEditor.ready(function (K) {
        var uploadbutton = K.uploadbutton({
            button: K('#uploadButton')[0],
            fieldName: 'imgFile',
            url: '../../editor/php/upload_json.php?dir=image',
            afterUpload: function (data) {
                if (data.error === 0) {
                    var url = K.formatUrl(data.url, 'absolute');
                    var host = location.host;
                    url = 'http://www.weiyouqu.net' + url;
                    K('#img').val(url);
                    $.post("../../editor/php/dunling.php", {
                        url: url
                    }, function (slta, sltb) {
                        //****//
                    });
                } else {
                    alert(data.message);
                }
            },
            afterError: function (str) {
                alert('自定义错误信息: ' + str);
            }
        });
        uploadbutton.fileBox.change(function (e) {
            uploadbutton.submit();
        });
        var uploadbutton2 = K.uploadbutton({
            button: K('#uploadButton2')[0],
            fieldName: 'imgFile',
            url: '../../editor/php/upload_json.php?dir=image',
            afterUpload: function (data) {
                if (data.error === 0) {
                    var url = K.formatUrl(data.url, 'absolute');
                    var host = location.host;
                    url = 'http://www.weiyouqu.net' + url;
                    K('#img2').val(url);
                    $.post("../../editor/php/dunling.php", {
                        url: url
                    }, function (slta, sltb) {
                        //****//
                    });
                } else {
                    alert(data.message);
                }
            },
            afterError: function (str) {
                alert('自定义错误信息: ' + str);
            }
        });
        uploadbutton2.fileBox.change(function (e) {
            uploadbutton2.submit();
        });
        var uploadbutton3 = K.uploadbutton({
            button: K('#uploadButton3')[0],
            fieldName: 'imgFile',
            url: '../../editor/php/upload_json.php?dir=image',
            afterUpload: function (data) {
                if (data.error === 0) {
                    var url = K.formatUrl(data.url, 'absolute');
                    var host = location.host;
                    url = 'http://www.weiyouqu.net' + url;
                    K('#img3').val(url);

                    $.post("../../editor/php/dunling.php", {
                        url: url
                    }, function (slta, sltb) {
                        //****//
                    });
                } else {
                    alert(data.message);
                }
            },
            afterError: function (str) {
                alert('自定义错误信息: ' + str);
            }
        });
        uploadbutton3.fileBox.change(function (e) {
            uploadbutton3.submit();
        });
    });
    $(function(){
        $("#on_type").change(function(){
            $.post("get_xia_type.php",{value:this.value},function(data){
                     if(data==0){
                         alert("获取分类信息错误");
                     }else{
                         var type_arr="<option>请选择子类</option>";
                         for(var o in data){
                             data[o].number++;
                             type_arr=type_arr+"<option value="+ data[o].number +">"+ data[o].name+"</option>";
                         }

                         $("#xia_type").html("<select name='code'>"+type_arr+"</select>");
                     }
            },'json')
        });
    })
</script>
<div class="container-fluid">

    <div class="row-fluid">
        <?php
        include('left.php'); ?>
        <div class="span9">
            <h1 class="page-title">文章编辑</h1>
            <!--
            <div class="btn-toolbar">
                <a href="#myModal" data-toggle="modal" class="btn">Delete</a>
              <div class="btn-group">
              </div>
            </div>
            -->
            <div class="well">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#home" data-toggle="tab">发布</a></li>
                    <li><a href="<?php echo $site ?>/detail.php?aid=<?php echo $id ?>" target="_blank">浏览查看</a></li>
                </ul>
                <div id="myTabContent" class="tab-content">
                    <div class="pull-right">

                        <a href="<?php echo $img_array[2] ?>"
                           target="_blank"><img
                                src="<?php echo "http://read.html5.qq.com/image?src=fav&imgflag=2&imageUrl=http://".$config['img_url']."/".$img_array[2] ?>" width="150" height="150"
                                border=0></a></div>
                    <div class="pull-right">
                        <a href="<?php echo $img_array[1] ?>"
                                               target="_blank"><img
                                src="<?php echo "http://read.html5.qq.com/image?src=fav&imgflag=2&imageUrl=http://".$config['img_url']."/".$img_array[1] ?>" width="150" height="150"
                                border=0></a></div>
                    <div class="pull-right">
                        <a href="<?php echo $img_array[0] ?>"
                           target="_blank"><img
                                src="<?php echo $img_array[0] ?>" width="150" height="150"
                                border=0></a></div>
                    <div class="tab-pane active in" id="home">
                        <form id="tab" action="edit_article.php" method="post">
                            <input type="hidden" name="id" value="<?php echo $id ?>">
                            <label>标题</label>
                            <input type="text" value="<?php echo $row[0]['title'] ?>" style="width:250px" name="title">
                            <label>缩略图</label>
                            <input style="width: 250px;" type="text" id="img" value="<?php echo $img_array[0]=str_replace("http://read.html5.qq.com/image?src=fav&imgflag=2&imageUrl=http://".$config['img_url'],"",$img_array[0])?>"
                                   name="pic"/>
                            <input type="button" id="uploadButton" value="选择图片"/>
                            <label>缩略图2</label>
                            <input style="width: 250px;" type="text" id="img2" value="<?php echo $img_array[1]?>"
                                   name="pic2"/>
                            <input type="button" id="uploadButton2" value="选择图片"/>
                            <label>缩略图3</label>
                            <input style="width: 250px;" type="text" id="img3" value="<?php echo $img_array[2]?>"
                                   name="pic3"/>
                            <input type="button" id="uploadButton3" value="选择图片"/>
                            <label>浏览次数(-1，留空，表示无限)</label>
                            <input type="text" value="<?php echo $row[0]['pv_max'] ?>" style="width:200px"
                                   name="pv_max"> 累计积分：<?php echo $row[0]['pv'] * $row[0]['money'] ?>积分
                            <label>分类</label>

                            <select name="type" id="on_type">
                                <?php
                                $row_list = $mysql->query("select * from typedata ");
                                foreach ($row_list as $key) {
                                    $type = $key['type'];
                                    if ($type == $row[0]['type']) {
                                        echo "<option value='$type' selected>" . $key['name'] . "</option>";
                                    } else {
                                        echo "<option value='$type'>" . $key['name'] . "</option>";
                                    }
                                } ?>
                            </select>
                            <div id="xia_type">
                             <label>标签</label>
                                <select name="code">
                                    <?php $type = $row[0]['type'];
                                    $row_list = $mysql->query("select number,name from codedata where type=$type-1");
                                    foreach ($row_list as $key) {
                                        $code = $key['number']+1;
                                        if ($code == $row[0]['code']) {
                                            echo "<option value='$code' selected>" . $key['name'] . "</option>";
                                        } else {
                                            echo "<option value='$code'>" . $key['name'] ."</option>";
                                        }
                                    } ?>
                                </select>
                            </div>
                            <label>标志</label>
                            <select id="flag" name="flag" >
                                <?php
                                    $flag_arr=array("普通","轮播","推送","推荐");
                                    for($i=0;$i<4;$i++){
                                        if($row[0]['flag']==$i) {
                                            echo "<option value=$i selected>$flag_arr[$i]</option>";
                                        }else{
                                            echo "<option value=$i>$flag_arr[$i]</option>";
                                        }
                                    }
                               ?>
                            </select>
                            <label>扩展</label>
                            <textarea name="extra"><?php echo $row[0]['extra'] ?></textarea>
                            <label>浏览单价：</label>
                            <input type="text" value="<?php echo $row[0]['money'] ?>" style="width:200px" name="money">
		<textarea value="Smith" rows="10" name="content" style="width:800px;height:400px;visibility:hidden;">
		<?php $html_content = str_replace('src="http://mmbiz', 'src="'.$image_protect.'http://mmbiz', $row[0]['content']);
        $html_content = str_replace('src="https://mmbiz', 'src="'.$image_protect.'https://mmbiz', $row[0]['content']);
        echo $html_content; ?>
		</textarea>
                            <label></label>
                            <button class="btn btn-primary"><i class="icon-save"></i> 保存</button>
                        </form>
                    </div>
                </div>
            </div>

            <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                 aria-hidden="true">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Delete Confirmation</h3>
                </div>
                <div class="modal-body">

                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete
                        the user?</p>
                </div>
                <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                </div>
            </div>

        </div>
    </div>


</div>

<footer>
    <hr>

    <p class="pull-right"><!--power by right--></p>


    <p><!--power by --></p>
</footer>


<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="lib/bootstrap/js/bootstrap.js"></script>
</body>
</html>